<template>
  <div>
    <el-dialog
      title="新增成员"
      width="480px"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <div class="dialogBody">
        <div class="searchBox">
          <span>姓名：</span>
          <el-autocomplete
            popper-class="my-autocomplete"
            v-model="input"
            :fetch-suggestions="querySearchAsync"
            placeholder="请输入"
            @select="handleSelect"
          >
            <template slot-scope="{ item }">
              <div v-if="item.id == -1">无匹配结果</div>
              <div v-else>
                <div class="name">{{ item.userName }}</div>
                <!-- <div class="addr">联系电话：{{ item.mobile }}</div>
                <div class="addr">居住房屋：{{ item.house.name }}</div>
                <div class="addr">身份证号：{{ item.idNo }}</div> -->
              </div>
            </template>
          </el-autocomplete>
        </div>
        <div class="userInfo" v-if="infoShow">
          <div class="addr">联系电话：{{ userInfo.mobile }}</div>
          <div class="addr">居住房屋：{{ userInfo.house.name }}</div>
          <div class="addr">身份证号：{{ userInfo.idNo }}</div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { userList } from "@/api/activity.js";
import { addClubMember } from "@/api/club.js";
export default {
  name: "",
  props: ["dialogVisible"],
  data() {
    return {
      input: "",
      userInfo: {
        house: {},
      },
      infoShow: false,
    };
  },

  components: {},

  computed: {},

  beforeMount() {},

  mounted() {},

  methods: {
    async querySearchAsync(queryString, cb) {
      if (queryString && queryString.length > 0) {
        this.input = queryString;
        try {
          const data = await userList({
            keywords: this.input,
          }); //search定义在data里
          console.log(data);
          // 赋值给建议列表，渲染到页面
          let list = data.data;
          // 如果list.length等于0，则代表没有匹配到结果。手动给list添加一条提示信息
          if (list.length == "0") {
            list.push({
              id: "-1",
              value: "无匹配结果",
            });
            // 调用 callback 返回建议列表的数据
            cb(list);
          } else {
            // list = list.map((item) => {
            //   return {
            //     value: `${item.userName}`,
            //     id: `${item.id}`,
            //   };
            // });
            // 调用 callback 返回建议列表的数据
            cb(list);
          }
        } catch (error) {
          console.log(error);
        }
      } else {
        cb([]);
      }
    },
    handleSelect(val) {
      this.input = val.userName;
      this.userInfo = val;
      this.infoShow = true;
    },
    onSubmit() {
      addClubMember({
        id: this.$route.query.clubId,
        uid: this.userInfo.id,
      }).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: res.msg,
          });
          this.$emit("handleClose", true);
        }
      });
    },
    handleClose() {
      this.$emit("handleClose");
    },
  },

  watch: {},
};
</script>
<style lang="scss" scoped>
.dialogBody {
  .userInfo {
    margin-left: 10px;
    margin-top: 20px;
    line-height: 24px;
  }
  .el-autocomplete {
    width: 90%;
  }
}
</style>
<style lang="scss">
.my-autocomplete {
  li {
    line-height: 20px;
    padding: 7px;
    > div {
      text-overflow: ellipsis;
      overflow: hidden;
    }

    .addr {
      font-size: 14px;
      color: #999;
    }
  }
}
</style>
